package com.digitalchina.industry.controller;

import com.digitalchina.common.data.RtnData;
import com.digitalchina.industry.service.IndustryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fanbo on 2017-07-13.
 */
@Controller
@RequestMapping("/industryController")
public class IndustryController {

    @Autowired
    private IndustryService industryService;

    /**
     * 查询行业列表
     * @param pageSize
     * @param pageNum
     * @param industryName
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/list.html")
    public String industrylList(@RequestParam(required = false, defaultValue = "5") long pageSize,
                                @RequestParam(required = false, defaultValue = "1") long pageNum,
                                @RequestParam(required = false) String industryName, ModelMap map,HttpServletRequest request) {
        getList(industryName,pageNum,pageSize,map);
        return "industry/industry_list";
    }

    /**
     * 行业增加
     * @param pageSize
     * @param pageNum
     * @param industryClass
     * @param industryNameAdd
     * @param industryName
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/addIndustry.html")

    public String addIndustry(@RequestParam(required = false, defaultValue = "5") long pageSize,
                              @RequestParam(required = false, defaultValue = "1") long pageNum,
                              @RequestParam(required = false) String industryClass,
                              @RequestParam(required = false) String industryNameAdd,
                              @RequestParam(required = false) String industryName, ModelMap map,HttpServletRequest request) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("industryName", industryNameAdd);
        params.put("industryClass", industryClass);
        industryService.addIndustry(params);
        getList(industryName,pageNum,pageSize,map);
        return "industry/industry_list";
    }

    /**
     * 行业更新
     * @param pageSize
     * @param pageNum
     * @param industryName
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/editIndustry.html")
    public String editIndustry(@RequestParam(required = false, defaultValue = "5") long pageSize,
                              @RequestParam(required = false, defaultValue = "1") long pageNum,
                              @RequestParam(required = false) String industryIdEdit,
                              @RequestParam(required = false) String industryClassEdit,
                              @RequestParam(required = false) String industryNameEdit,
                              @RequestParam(required = false) String industryName, ModelMap map,HttpServletRequest request) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("industryName", industryNameEdit);
        params.put("industryClass", industryClassEdit);
        params.put("id", industryIdEdit);
        industryService.editIndustry(params);
        getList(industryName,pageNum,pageSize,map);
        return "industry/industry_list";
    }

    /**
     * 行业删除
     * @param deleteId
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/deletIndustry.html")
    public RtnData deletIndustry(@RequestParam(required = true) String deleteId, ModelMap map, HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", Integer.valueOf(deleteId));
        try {
            industryService.deletIndustry(params);
        }catch (Exception e){
            e.printStackTrace();
            return RtnData.fail(true);
        }
        return  RtnData.ok(true);
    }

    @RequestMapping(value = "/checkIndustryName.html", method = RequestMethod.GET)
    @ResponseBody
    public RtnData checkIndustryName(@RequestParam(required = true) String industryName, ModelMap map){
        Map<String, Object> params = new HashMap<>();
        params.put("industryName", industryName);
        try {
            List<Map<String,Object>> rtnList = industryService.checkIndustryName(params);
            if(rtnList!=null && rtnList.size()>0){
                return RtnData.fail(true);
            }
        }catch (Exception e){
            e.printStackTrace();
            return RtnData.fail(true);
        }
        return  RtnData.ok(true);
    }


    public ModelMap getList(String industryName,long pageNum,long pageSize,ModelMap map){

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("industryName", industryName);
        int totalSize = industryService.findTotalCount(params);
        params.put("startIndex", (pageNum - 1) * pageSize);
        params.put("pageS", pageSize);
        List industrys = industryService.getAllIndustrylList(params);
        map.put("industrys", industrys);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("totelPage", totalSize % pageSize > 0 ? totalSize / pageSize + 1 : totalSize / pageSize);
        map.put("currentPage", pageNum);
        map.put("industryName",industryName);
        return map;
    }
}
